<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${appProperty.basePackage}.dao.${tableMeta.upperName}DAO">

    <resultMap id="resultMap" type="${appProperty.basePackage}.model.dto.${tableMeta.upperName}DTO">
<#if columnList?exists>
    <#list columnList as column>
    <#if column.primaryKey == 1 >
        <id column="${column.name}" property="${column.camelName}" />
    <#else>
        <result column="${column.name}" property="${column.camelName}" />
    </#if>
    </#list>
</#if>
    </resultMap>

    <sql id="columnList">
    <#if columnNames?exists>
        ${columnNames}
    </#if>
    </sql>

    <insert id="insert" parameterType="${appProperty.basePackage}.model.dto.${tableMeta.upperName}DTO" useGeneratedKeys="true"
            keyProperty="id" keyColumn="id">
        INSERT INTO ${tableMeta.name}
        <trim prefix="(" suffix=")" suffixOverrides=",">
    <#if columnList?exists>
        <#list columnList as column>
            <if test="${column.camelName} != null">
                ${column.name},
            </if>
        </#list>
    </#if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
    <#if columnList?exists>
        <#list columnList as column>
            <if test="${column.camelName} != null">
            ${r"#"}{${column.camelName}},
            </if>
        </#list>
    </#if>
        </trim>
    </insert>

    <update id="deleteById" parameterType="java.lang.Long">
        UPDATE ${tableMeta.name} set deleted = 1
        WHERE id = ${r"#"}{id}
    </update>

    <update id="updateById" parameterType="${appProperty.basePackage}.model.dto.${tableMeta.upperName}DTO">
        UPDATE ${tableMeta.name}
        <set>
    <#if columnList?exists>
        <#list columnList as column>
            <if test="${column.camelName} != null">
                ${column.name} = ${r"#"}{${column.camelName}},
            </if>
        </#list>
    </#if>
        </set>
        WHERE id = ${r"#"}{id}
    </update>

    <select id="selectById" parameterType="java.lang.Long" resultMap="resultMap">
        SELECT
        <include refid="columnList" />
        FROM ${tableMeta.name}
        WHERE id = ${r"#"}{id} and deleted = 0
    </select>

    <select id="selectByQuery" parameterType="${appProperty.basePackage}.model.query.${tableMeta.upperName}Query" resultMap="resultMap">
        SELECT
        <include refid="columnList" />
        FROM ${tableMeta.name}
        <where>
            deleted = 0
    <#if columnList?exists>
        <#list columnList as column>
            <if test="${column.camelName!} != null">
                and ${column.name} = ${r"#"}{${column.camelName!}}
            </if>
        </#list>
    </#if>
        </where>
        <if test="orderBy != null and orderType != null">
            ORDER BY ${r"$"}{orderBy} ${r"$"}{orderType}
        </if>
        <if test="limit != null and offset != null">
            LIMIT ${r"#"}{offset},${r"#"}{limit}
        </if>
    </select>

    <select id="selectPageByQuery" parameterType="${appProperty.basePackage}.model.query.${tableMeta.upperName}Query" resultMap="resultMap">
        SELECT
        <include refid="columnList" />
        FROM ${tableMeta.name}
        <where>
            deleted = 0
    <#if columnList?exists>
        <#list columnList as column>
            <if test="${column.camelName} != null">
            and ${column.name} = ${r"#"}{${column.camelName!}}
            </if>
        </#list>
    </#if>
        </where>
        <if test="orderBy != null and orderType != null">
            ORDER BY ${r"$"}{orderBy} ${r"$"}{orderType}
        </if>
        <if test="limit != null and offset != null">
            LIMIT ${r"#"}{offset},${r"#"}{limit}
        </if>
    </select>

    <select id="selectCountByQuery" parameterType="${appProperty.basePackage}.model.query.${tableMeta.upperName}Query" resultType="java.lang.Integer">
        SELECT
        COUNT(*)
        FROM ${tableMeta.name}
        <where>
            deleted = 0
    <#if columnList?exists>
        <#list columnList as column>
            <if test="${column.camelName} != null">
                and ${column.name} = ${r"#"}{${column.camelName}}
            </if>
        </#list>
    </#if>
        </where>
    </select>

</mapper>
